﻿@model ProductOverviewModel
@using Nop.Web.Models.Catalog;
@using Nop.Core.Domain.Catalog;
@using System.Text.RegularExpressions;
@{
    //prepare "Add to cart" AJAX link
    string addtocartlink = "";
    if (Model.ProductPrice.ForceRedirectionAfterAddingToCart)
    {
        addtocartlink = Url.RouteUrl("AddProductToCart", new { productId = Model.Id, forceredirection = Model.ProductPrice.ForceRedirectionAfterAddingToCart });
    }
    else
    {
        addtocartlink = Url.RouteUrl("AddProductToCart", new { productId = Model.Id });
    }
}
 
<div class="col-xs-12 col-sm-6 col-md-3 product-item thumbnail" data-productid="@Model.Id">

       
           
        <div class="product-picture">

            <a href="@Url.RouteUrl("Product", new { SeName = Model.SeName })">
                <img alt="@Model.DefaultPictureModel.AlternateText" src="@Model.DefaultPictureModel.ImageUrl" />
            </a>
        </div>


    <div class="product-info">
        <h4 class="product-title">
            <a href="@Url.RouteUrl("Product", new { SeName = Model.SeName })">@Model.Name</a>
        </h4>
        <div class="product-desc">
            <p>@Model.ShortDescription</p>
        </div>
        <div class="product-price">
            
            <span class="current-price @if (!String.IsNullOrEmpty(Model.ProductPrice.OldPrice)){<text>discount</text>}">@Model.ProductPrice.Price</span>
            @if (!String.IsNullOrEmpty(Model.ProductPrice.OldPrice))
            {
                var reg = new Regex(@"([\d,.]+)");
                var matchNew = reg.Match(Model.ProductPrice.Price).Value;
                var matchOld = reg.Match(Model.ProductPrice.OldPrice).Value;
                float floatNew = (float)Convert.ToDouble(matchNew);
                float floatOld = (float)Convert.ToDouble(matchOld);
                if (floatOld > floatNew) 
                {
                    <span class="old-price">&nbsp; @Model.ProductPrice.OldPrice &nbsp;</span>
                }
            }
            @if (Model.ProductReview != null)
            {

                //Rating CSS Classes
                int totalreviews = Model.ProductReview.TotalReviews;
                float ratingsum = 0;
                if (totalreviews != 0) {
                    ratingsum = (float)Model.ProductReview.RatingSum / totalreviews;
                }
                float maxrating = Rating.MaxRating;
                float rating = (float)(ratingsum / maxrating) * 5;
                var ratingBase = Math.Floor(rating);
                var ratingTop = (rating - ratingBase) * 10;
                int ratingTopRound = Convert.ToInt32(ratingTop);
                var starFirst = 0;
                var starSecond = 0;
                var starThird = 0;
                var starFourth = 0;
                var starFifth = 0;
                if (ratingBase >= 0)
                {
                    if (ratingBase >= 1)
                    {
                        starFirst = 10;
                        if (ratingBase >= 2)
                        {
                            starSecond = 10;
                            if (ratingBase >= 3)
                            {
                                starThird = 10;
                                if (ratingBase >= 4)
                                {
                                    starFourth = 10;
                                    if (ratingBase >= 5)
                                    {
                                        starFifth = 10;
                                    }
                                    else
                                    {
                                        starFifth = ratingTopRound;
                                    }
                                }
                                else
                                {
                                    starFourth = ratingTopRound;
                                }
                            }
                            else
                            {
                                starThird = ratingTopRound;
                            }
                        }
                        else
                        {
                            starSecond = ratingTopRound;
                        }
                    }
                    else
                    {
                        starFirst = ratingTopRound;
                    }
                }
                else { }
                

    
                <div class="product-rating" title="@Model.ProductReview.RatingAverage">
                    @if (Model.ProductReview.TotalReviews > 0)
                    { 
                    <div class="rating-stars">
                        <div class="star-base"><div class="star-top rate-@starFirst"></div></div>
                        <div class="star-base"><div class="star-top rate-@starSecond"></div></div>
                        <div class="star-base"><div class="star-top rate-@starThird"></div></div>
                        <div class="star-base"><div class="star-top rate-@starFourth"></div></div>
                        <div class="star-base"><div class="star-top rate-@starFifth"></div></div>
                    </div>
                    }
                </div>
               
            } 
        </div>
        
        @if (!Model.ProductPrice.DisableBuyButton)
        {
            <div class="buttons">
                @if (!Model.ProductPrice.AttributesRequiredBeforeAddingToCart)
                {
                    <a href="@Url.RouteUrl("Product", new { SeName = Model.SeName })" class="cart-add-product" data-id="@Model.Id" data-link="@addtocartlink"><i class="fa fa-shopping-cart"></i> @(Model.ProductPrice.AvailableForPreOrder ? T("ShoppingCart.PreOrder") : T("ShoppingCart.AddToCart"))</a>
                }
                else
                {
                    <a href="@Url.RouteUrl("Product", new { SeName = Model.SeName })" class="product-link" data-id="@Model.Id" data-link="@addtocartlink"><i class="fa fa-shopping-cart"></i> @(Model.ProductPrice.AvailableForPreOrder ? T("ShoppingCart.PreOrder") : T("ShoppingCart.AddToCart"))</a>
                }
            </div>
        }
    </div>
</div>
